#!/bin/bash
#
# Description: Create scaled mask and macro from a virtual slide
# 
# The MIT License (MIT)
# Copyright (c) 2014-2016, Bas G.L. Nelissen, UMC Utrecht, the Netherlands.
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 

# Variables
SCRIPTNAME=$(basename $0)
DESCRIPTIONSHORT="Create scaled *.mask.png and *.macro.png from a virtual slide."
DEPENDENCIES=("convert")
SUPPORTED_FORMATS=("TIFF" "Aperio" "Hamamatsu" "iScan" "iScanHT" "Leica")
DEFAULT_MASKSUFFIX=".mask"
DEFAULT_MACROSUFFIX=".macro"
DEFAULT_MACRO_THUMBSIZE="2000x2000" # Maximum size of macro image
DEFAULT_QSUBARGS="-pe threaded 1 -q veryshort" # THIS NEEDS UPDATING DUE TO THE NEW CENTOS7 SYSTEM
DEFAULT_MASKARGS="-blur 40x40 -fuzz 4%"

# Errors go to stderr
err() {
  echo "${SCRIPTNAME} ERROR: $@" >&2
}

# usage message
usage() {
cat <<- EOF
$SCRIPTNAME --help for more information.
EOF
}

# version info
version() {
cat << EOF
slideToolkit 0.1 beta
($SCRIPTNAME is part of the slideToolkit)

The MIT License (MIT) <http://opensource.org/licenses/MIT>
Copyright (c) 2014-2016, Bas G.L. Nelissen, UMC Utrecht, the Netherlands.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

There is NO WARRANTY, to the extent permitted by law.

Written by Bas G.L. Nelissen | https://github.com/bglnelissen.
EOF
}


# help message
helpMessage() {
cat <<- EOF
${SCRIPTNAME}: ${DESCRIPTIONSHORT}

usage:
  $SCRIPTNAME [options] -f <filename>

options:
  -f, --file <filename>         virtual slide to create mask from

  [--masksuffix] <text>         mask suffix ["$DEFAULT_MASKSUFFIX"]
  [--macrosuffix] <text>        macro suffix ["$DEFAULT_MACROSUFFIX"]
  [-l, --layer] <integer>       force specific layer by layer id
  [--mask-args] <arguments>     mask arguments (imageMagick) ["$DEFAULT_MASKARGS"]
  [--ignore-dependencies]       ignores all dependencies, but gives warning
  
  --help                        display this help
  --version                     display version and license information


examples:
  $SCRIPTNAME "file.tif"
  $SCRIPTNAME  --file="file.tif"

print a job list:
  find "\$(pwd)" -iname "*.TIF" -exec $(command -v "$SCRIPTNAME") -f "{}" \;

print a job list for 'qsub':
  find "\$(pwd)" -iname "*tif" -type f \\
        -exec printf "qsub $DEFAULT_QSUBARGS " \; \\
        -exec printf "\$(command -v "$SCRIPTNAME") " \; \\
        -exec echo "--file=\""{}"\" " \;

dependencies: ${DEPENDENCIES[@]}
supported formats: ${SUPPORTED_FORMATS[@]}

You can mask parts of your image that you do not wat to process. This
program will use 'convert' to create a mask for you which you can later
edit if needed. The mask is a layer within the original TIF file. The
slideToolkit excludes real black in further processing (color #000000).
Two new files will be created, one file.macro.png, and one masked macro
file (file.mask.png). Definitions of masking can be set with the
--mask-args flag. A blur creates space between the mask and the tissue.
A fuzz selects the background. A fuzz of 3% works well, but fails on
very light slides, a fuzz of 2% is mightby to easy on the selection, 5%
works on slides with a darkish background. Pipe the 'multiple folders at
once' output in 'parallel' for parallel processing. For some slides, the
virtual slide format is not known. For these instances you need to run
slideInfo to find the correct layer manually. When the mask layer is
identified (approximately 2000x2000 pixels), use the --layer flag.

The slideToolkit and all its tools are released under the terms of the MIT license.
The slideToolkit (C) 2014-2016, Bas G.L. Nelissen, UMC Utrecht, the Netherlands.
Report issues at https://github.com/bglnelissen/slideToolkit/issues.

EOF
}
# Menu
# Empty variables
FILE=""
LAYER=""
MASKSUFFIX=""
MACROSUFFIX=""
MASKARGS=""
# illegal option
illegalOption() {
cat <<- EOF
$SCRIPTNAME: illegal option $1
$(usage)
EOF
exit 1
}
# loop through options
while :
do
  case $1 in
    -h)
      usage
      exit 0 ;;
    --help | -\?)
      helpMessage
      exit 0 ;;
    --version )
      version
      exit 0 ;;
    -f | --file)
      FILE=$2
      shift 2 ;;
    --file=*)
      FILE=${1#*=}
      shift ;;
    -l | --layer)
      LAYER=$2
      shift 2 ;;
    --layer=*)
      LAYER=${1#*=}
      shift ;;
    --masksuffix)
      MASKSUFFIX=$2
      shift 2 ;;
    --masksuffix=*)
      MASKSUFFIX=${1#*=}
      shift ;;
    --mask-args)
      MASKARGS=$2
      shift 2 ;;
    --mask-args=*)
      MASKARGS=${1#*=}
      shift ;;
    --macrosuffix)
      MACROSUFFIX=$2
      shift 2 ;;
    --macrosuffix=*)
      MACROSUFFIX=${1#*=}
      shift ;;
     --ignore-dependencies)
      IGNOREDEPENDENCIES=TRUE
      shift ;;
    --) # End of all options
      shift
      break ;;
    -*)
      illegalOption "$1"
      shift ;;
    *)  # no more options. Stop while loop
      break ;;
  esac
done
# DEFAULTS
# set FILE
if [ "$FILE" != "" ]; then
  FILE="$FILE"
else
  FILE="$1"
fi
# set MASKSUFFIX default
if [ "$MASKSUFFIX" != "" ]; then
  MASKSUFFIX="$MASKSUFFIX"
else
  MASKSUFFIX="$DEFAULT_MASKSUFFIX"
fi
# set MASKARGS default
if [ "$MASKARGS" != "" ]; then
  MASKARGS="$MASKARGS"
else
  MASKARGS="$DEFAULT_MASKARGS"
fi
# set MACROSUFFIX default
if [ "$MACROSUFFIX" != "" ]; then
  MACROSUFFIX="$MACROSUFFIX"
else
  MACROSUFFIX="$DEFAULT_MACROSUFFIX"
fi
# set IGNOREDEPENDENCIES
if [[ "$IGNOREDEPENDENCIES" =~ (true|TRUE|YES|yes) ]] ; then
  IGNOREDEPENDENCIES=true
else
  IGNOREDEPENDENCIES=false
fi

# requirements
checkRequirements() {
  if ! [[ -f "$FILE" ]] ; then
    err "No such file: $FILE"
    usage
    exit 1
  fi
}

# Dependencies
checkDependencies(){
  DEPS=""
  DEPS_FAIL="0"
  for DEP in ${DEPENDENCIES[@]}; do
    if [[ 0 != "$(command -v "$DEP" >/dev/null ;echo "$?")" ]]; then
       err "Missing dependency: $DEP"
       DEPS_FAIL=$(($DEPS_FAIL + 1))
    fi
  done
  if [[ "$DEPS_FAIL" > 0 ]]; then
    # only warning when --ignore-dependencies is set.
    if [[ "$IGNOREDEPENDENCIES" == "true" ]]; then
      err "Ignoring missing dependencies (${DEPS_FAIL}), continue..."
    else
      err "Fix missing dependencies (${DEPS_FAIL}), exit."
      usage
      exit 1
    fi
  fi
}
# get mask function for each type of scanner
getMacro(){
  # get thumb by layer id
  LAYER="$LAYER"
	convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC"  "$MACRO"
}
getMacroiScanHT(){
  # layer 8 contains the macro
  LAYER=8
  convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC"  "$MACRO"
}
getMacroAperio(){
  # the third to last layer contains the macro
  NR_LAYERS=`identify "$FILE" | wc -l`
  LAYER="$(($NR_LAYERS - 3))"
  convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC"  "$MACRO"
}
getMacroLeica(){
  LAYER=5 # determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
  convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC"  "$MACRO"
}
getMacroHamamatsu(){
  LAYER=5 # determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
  convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC"  "$MACRO"
}
getMacroiScan(){
  LAYER=7 # determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
  convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC"  "$MACRO"
}

# actual program
programOutput(){
  # set variables
  LAYER="$LAYER"
  FILEFULL="$(echo "$(cd "$(dirname "$FILE")"; pwd)"/"$(basename "$FILE")")" # full path $FULL
  BASENAME="$(basename "$FILEFULL")"    # basename
  EXTENSION="${BASENAME##*.}"         # extension
  FILEPATH="${FILEFULL%.*}"           # full path, no extension
  FILENAME="${BASENAME%.*}"           # filename, no extension"
  MASK="${FILEPATH}${MASKSUFFIX}.png"
  MACRO="${FILEPATH}${MACROSUFFIX}.png"

  # check metadata for scanner data
  if [ "$LAYER" != "" ]; then
    getMacro # layer is set, do it the easy way
  elif [[ "0" < "$(tiffinfo  "$FILEFULL" 2>&1 | strings | grep -i "iScanHT" | wc -l | awk '{print $1}' )" ]]; then
    getMacroiScanHT # iScanHT found
  elif [[ "0" < "$(tiffinfo  "$FILEFULL" 2>&1 | strings | grep -i "Aperio" | wc -l | awk '{print $1}')" ]]; then
    getMacroAperio  # Aperio found
  elif [[ "0" < "$(tiffinfo  "$FILEFULL" 2>&1 | strings | grep -i "Leica" | wc -l | awk '{print $1}')" ]]; then
    getMacroLeica  # Leica found
  elif [[ "0" < "$(tiffinfo  "$FILEFULL" 2>&1 | strings | grep -i "Hamamatsu" | wc -l | awk '{print $1}')" ]]; then
    getMacroHamamatsu  # Hamamatsu found
  elif [[ "0" < "$(tiffinfo  "$FILEFULL" 2>&1 | strings | grep -i "iScan" | wc -l | awk '{print $1}')" ]]; then
    getMacroiScan  # Ventana iScan found
  else    err "Unknown virtual slide format: $FILEFULL"
    err "You probably want to set --layer manually."
    err "Run slideInfo for layer information."
    usage
    exit 1
  fi
  
  # what am I doing here?
  # create mask, lay it ontop of the macro image.
  # a fuzz of 4% works well, but fails on very light slides
  # a fuzz of 2% is might by to easy on the selection
  # a fuzz of 2.5% is possible (but still to stringend on some slides)
  # - we add a temporary border to the image of 100 pixels.
  # - The image is blurred, the border will make sure the blur is applied to the borders as well. 
  #   A blur will fill small holes in the tissue and will get rid of dust speckles.
  # - Using fuzz, the background is selected and made transparent.
  # - the previous added border is removed
  # - the alpha makes all transparent parts of the image black, and the rest white.
  # - now everything white is converted to transparent.
  # - this image is now put on-top of the original one.

  convert "$MACRO" \( "$MACRO" \
    -bordercolor white -border 100x100 \
    $MASKARGS -transparent white \
    -crop +100+100 -crop -100-100 +repage \
    -alpha extract \
    -transparent white \) \
    -flatten "$MASK"
}
# all check?
checkRequirements
checkDependencies 

# lets go!
# actual program
programOutput